est drop _all
graph drop _all

* mainpath should be set to the Replication package base directory
global mainpath ".."


/****** Naive forecaster-specific regression procedure ********/
use "$mainpath\data\bc_May23.dta", clear
gen ym=mofd(date)
egen ff_count = count(ff), by(id ym)
egen gap_count = count(gap), by(id ym)
egen cpi_count = count(cpi), by(id ym)
keep if ff_count >=3
keep if gap_count >=3
keep if cpi_count >=3
egen forecaster_month = group(id ym)

tempfile coeffs
statsby _b _se, by(forecaster_month) saving(`coeffs'): reg ff gap cpi
tempfile month
statsby month = r(mean), by(forecaster_month) saving(`month'): sum ym

use `coeffs', clear
merge m:1 forecaster_month using `month'

sum _b_gap, d
* drop one large outlier
drop if _b_gap < -100

collapse (mean) _b_gap (p25) gap25 = _b_gap (p75) gap75 = _b_gap, by(month)

rename _b_gap gamma_ew_baseline
rename month ym
tempfile gamma_ew_baseline
save `gamma_ew_baseline', replace


use "$mainpath\data\bc_May23.dta", clear
gen ym=mofd(date)
egen forecaster_month = group(id ym)
tsset forecaster_month h
gen l_ff = l.ff

egen ff_count = count(ff), by(id ym)
egen gap_count = count(gap), by(id ym)
egen cpi_count = count(cpi), by(id ym)
keep if ff_count >=3
keep if gap_count >=3
keep if cpi_count >=3

statsby _b _se, by(forecaster_month) saving(`coeffs', replace): reg ff l_ff gap cpi

use `coeffs', clear
merge m:1 forecaster_month using `month'

sum _b_gap, d
drop if _b_gap < -100

collapse (mean) _b_gap, by(month)

rename month ym
rename _b_gap gamma_ew_inertial
tempfile gamma_ew_inertial
save `gamma_ew_inertial', replace


import delimited "$mainpath\output\bluechip_rule_regressions.csv", varnames(1) clear 
gen ym = mofd(date(date,"YMD"))
format ym %tm
order ym
tsset ym
tempfile gamma_regs
save `gamma_regs'

import delimited "$mainpath\output\bluechip_rule_inertial.csv", varnames(1) clear 
gen ym = mofd(date(date,"YMD"))
format ym %tm
order ym
tsset ym
rename gamma gamma_fe_inertial
tempfile gamma_ssm
save `gamma_ssm'

merge 1:1 ym using `gamma_regs'
drop _merge
merge 1:1 ym using `gamma_ew_baseline'
drop _merge
merge 1:1 ym using `gamma_ew_inertial'

label var gamma_ew_baseline "Equal-weight forecasters"
twoway line gamma_fe gamma_ew_baseline ym

gen gamma_ew = gamma_ew_baseline

save "$mainpath\output\gamma_ew",replace